package com.zytj.rs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Predicate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.zytj.rs.dao.IMemberDao;
import com.zytj.rs.dto.MemberDTO;
import com.zytj.rs.model.Member;

@Service
public class MemberService implements IMemberService {

	@Resource
	private IMemberDao memberDao;
	
	@Override
	public Map findAll(MemberDTO dto) {
		Sort sort = new Sort(Sort.Direction.ASC,"id");//排序
		Pageable pageable = new PageRequest(dto.getPage()-1, dto.getLimit(), sort);	
		Map<String,Object> map = new HashMap<String, Object>();
		Page<Member> page = memberDao.findAll(getWhereClause(dto),pageable);
		map.put("code", 0);
		map.put("count",memberDao.count(getWhereClause(dto)));
		map.put("data", page.getContent());
		return map;
	}
	
	private Specification<Member> getWhereClause(MemberDTO dto){
		return new Specification<Member>() {
			
			@Override
			public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query,CriteriaBuilder criteriaBuilder) {
				
				List<Predicate> list = new ArrayList<Predicate>();
				if(dto.getCategory()==0) {
					//系
				    if(dto.getDepartment()!=null && ""!=dto.getDepartment()) {
						list.add(criteriaBuilder.equal(root.get("department").as(String.class), dto.getDepartment()));
					}
				    //专业
					if(dto.getProfession()!=null && ""!=dto.getProfession()) {
						list.add(criteriaBuilder.equal(root.get("profession").as(String.class), dto.getProfession()));
					}
					//年级
					if(dto.getGrade()!=null && ""!=dto.getGrade()) {
						list.add(criteriaBuilder.equal(root.get("grade").as(String.class), dto.getGrade()));
					}
					//姓名模糊查询
					if(dto.getName()!=null &&""!=dto.getName()) {
						list.add(criteriaBuilder.like(root.<String>get("name"),"%"+dto.getName()+"%"));
					}
					//会员卡号
					if(dto.getCard_no()!= null && ""!=dto.getCard_no()) {
						list.add(criteriaBuilder.equal(root.get("card_no").as(String.class),dto.getCard_no()));
					}
					//电话
					if(dto.getPhone()!= null && ""!=dto.getPhone()) {
						list.add(criteriaBuilder.equal(root.get("phone").as(String.class),dto.getPhone()));
					}
					//最小时间
					if(dto.getMinTime()!= null && ""!= dto.getMinTime()) {
						list.add(criteriaBuilder.greaterThanOrEqualTo(root.<String>get("regdate"), dto.getMinTime()));
					}
					//最大时间
					if(dto.getMaxTime()!= null && ""!= dto.getMaxTime()) {
						list.add(criteriaBuilder.lessThanOrEqualTo(root.<String>get("regdate"), dto.getMaxTime()));
					}
				
				}else if(dto.getCategory()==1) {
					//姓名
					if(dto.getName()!=null &&""!=dto.getName()) {
						list.add(criteriaBuilder.like(root.<String>get("name"),"%"+dto.getName()+"%"));
					}
					//会员卡号
					if(dto.getCard_no()!= null && ""!=dto.getCard_no()) {
						list.add(criteriaBuilder.equal(root.get("card_no").as(String.class),dto.getCard_no()));
					}
					//电话
					if(dto.getPhone()!= null && ""!=dto.getPhone()) {
						list.add(criteriaBuilder.equal(root.get("phone").as(String.class),dto.getPhone()));
					}
					//最小时间
					if(dto.getMinTime()!= null && ""!= dto.getMinTime()) {
						list.add(criteriaBuilder.greaterThanOrEqualTo(root.<String>get("regdate"), dto.getMinTime()));
					}
					//最大时间
					if(dto.getMaxTime()!= null && ""!= dto.getMaxTime()) {
						list.add(criteriaBuilder.lessThanOrEqualTo(root.<String>get("regdate"), dto.getMaxTime()));
					}
				}
				Predicate[] pre = new Predicate[list.size()];
				return query.where((javax.persistence.criteria.Predicate[]) list.toArray(pre)).getRestriction();
			}
		};
		}
	}

